#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os, random, pygame
from utils import load_image
from pygame import *
from pokemon import *

pygame.init()

#black = 0, 0, 0
pygame.key.set_repeat(100, 100)
background = pygame.image.load("img/bg.png")
backgroundRect = background.get_rect()
size = (width, height) = background.get_size()
screen = pygame.display.set_mode(size)


    
def main():
    # POKEMONS                     
    poke1 = Charmeleon([213, 220])
    poke2 = Pidgey([329, 161])
    
    
    #cria os nossos objetos (bola e bouncer)
    pygame.display.set_caption('Figth!')
    clock = pygame.time.Clock()
    sprites = []

    while 1:
        #garante que o programa nao vai rodar a mais que 120fps
        clock.tick(300)
        
        #checa eventos de teclado
        for event in pygame.event.get():

            if event.type == pygame.QUIT: 
               sys.exit()

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_1:
                    poke2.life -=  random.randrange(1,10)
                    fire = Ember(poke2.get_pos(),poke1.get_pos())
                    sprites.append(fire)
                if event.key == pygame.K_2:
                    poke2.life -=  random.randrange(1,5)
                    fire = Scratch(poke2.get_pos(),poke1.get_pos())
                    sprites.append(fire)                    
                if event.key == pygame.K_0:
                    print 'evento'
                if event.key == pygame.K_KP_ENTER:
                    sys.exit()

            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1: 
                    print pygame.mouse.get_pos()
                if event.button == 2:
                    sprites = []
                #if event.button == 3:
                    #sprites.append(ball(pygame.mouse.get_pos()))
  
  
  
  
#LIFE
#@TODO: ENCAPSULAR
        life1 = pygame.Surface((poke1.life, 10))          
        life1.fill((255, 0, 0))
        life2 = pygame.Surface((poke2.life, 10))          
        life2.fill((159, 182, 205))
                 
        # Create a font
        font = pygame.font.Font(None, 16)
        # Render the text
        text_life1 = font.render(str(int(poke1.life)) , True, (255,255, 255), (255, 0,0))
        text_life2 = font.render(str(int(poke2.life)), True, (255,255, 255), (159, 182, 205))
        # Create a rectangle
        textLife1Rect = text_life1.get_rect()
        textLife2Rect = text_life2.get_rect()
        # Center the rectangle
        textLife1Rect.centerx, textLife1Rect.centery = 190, 255
        textLife2Rect.centerx , textLife2Rect.centery = 284, 128
        
        ex1 = pygame.Surface((poke1.experiencia, 5))          
        ex1.fill((47, 255, 95))
                 
        font = pygame.font.Font(None, 16)
        text_ex = font.render(str(int(poke1.experiencia)) , True, (255, 255, 255), (47, 255, 95))
        textEx1Rect = text_ex.get_rect()
        textEx1Rect.centerx, textEx1Rect.centery = 196, 267
# LIFE END 

#MUDAR POKE
        menuEsquerdo = pygame.image.load('img/batalha/esquerda.gif')          
        menuEsquerdoRect = menuEsquerdo.get_rect()
        menuEsquerdoRect.centerx , menuEsquerdoRect.centery = 40, 350
        
        menuDireito = pygame.image.load('img/batalha/direita.gif')          
        menuDireitoRect = menuDireito.get_rect()
        menuDireitoRect.centerx , menuDireitoRect.centery = 505, 351
               

        poke1.update()
        poke2.update() 
    

        #redesenha a tela
        screen.blit(background, backgroundRect)
        screen.blit(life1, (190, 250)) 
        screen.blit(life2, (284, 130)) 
        screen.blit(ex1, textEx1Rect) 
        screen.blit(text_life1,  textLife1Rect)                 
        screen.blit(text_life2,textLife2Rect)
        screen.blit(menuEsquerdo,menuEsquerdoRect)
        screen.blit(menuDireito,menuDireitoRect)
        
	pygame.transform.flip(poke1.image,1, 0)
	poke1.image.render(screen,(213,200))
       # screen.blit(poke1.image, poke1.rect)
        screen.blit(poke2.image, poke2.rect)
	
        for sprite in sprites:
            if sprite.fire_position_x == -100:
                sprites.remove(sprite)
            sprite.render(screen) 
        
        pygame.display.update() 
        pygame.display.flip()
        
            
if __name__ == "__main__":
    main()


